{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pycharm 如何用\n",
    "\n",
    "## linux 环境  linux 环境下怎么使用 \n",
    "\n",
    "## 如何下载包，安装包的\n",
    "\n",
    "##  文件的操作\n",
    "\n",
    "##  文件模式\n",
    "\n",
    "##  如何获取目录\n",
    "\n",
    "## print()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trans_code  ={\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\",\"6127\":\"雪龙果\"}\n",
    "search_code = input(\"请输入你需要查询的code: \")\n",
    "# 改成一个迭代器 那种 [] \n",
    "for key,value in trans_code.items():\n",
    "    #key 7125 7126\n",
    "    #value 苹果,香蕉\n",
    "    if search_code in key or search_code in value: #存在我走下面\n",
    "        print(key,value)\n",
    "    else:\n",
    "        print(\"我在第二行\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  文件的操作\n",
    "\n",
    "##  文件模式\n",
    "\n",
    "##  如何获取目录\n",
    "\n",
    "## print()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 文件就可以看作一个对象  \n",
    "\n",
    "## 增删改查 \n",
    "## 文件模式 \n",
    "\n",
    "## 读文件的需求，写文件的需求  \n",
    "  r  read \n",
    "  w  write \n",
    "  \n",
    "  a append  [追加的含义]\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上下文管理器\n",
    " with open('filenam.txt','mode',encoding='utf-8') as f:\n",
    "    f.read() # f.write()\n",
    "\n",
    "## 模式\n",
    "   a   appned  会往文本里面追加数据\n",
    "\n",
    "   w  write 会写文件,每次都清空\n",
    "\n",
    "   r  read  读文件\n",
    "\n",
    "   read() ,readlines() \n",
    "### 下一节实现一个上下文管理器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "苹果\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open('filename.txt','rt',encoding='utf-8') as f:\n",
    "    t =f.read() # f.write()\n",
    "\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这个是读取的内容 123\n",
      "123\n",
      "123\n",
      "123\n",
      "123\n",
      "123\n",
      "123\n",
      "123\n",
      "123\n",
      "123\n",
      "\n",
      "这是readlines 对象 123\n",
      "\n"
     ]
    }
   ],
   "source": [
    "## 写文件的模式 打开一个文件作为对象传递给f \n",
    "## filename  =filename.txt   'w' encodig='utf-8'\n",
    "\n",
    "##  w 模式 每次都会把数据进行清空,我有时候不想清空数据,我就想追加  a \n",
    "with open('filename.txt','a',encoding='utf-8') as  f:\n",
    "    ## 对他进行操作\n",
    "        f.write(\"123\\n\")\n",
    "\n",
    "\n",
    "### 打开文件\n",
    "f  = open('filename.txt','r')\n",
    "### 读文件\n",
    "ts = f.read() \n",
    "print(\"这个是读取的内容\",ts)\n",
    "### 关闭文件\n",
    "f.close()\n",
    "## 传递对象给f 的时候,他会创建一把锁，不让其他人访问\n",
    "with open(\"filename.txt\",\"r\",encoding='utf-8')  as  f:\n",
    "\n",
    "    #完成读的任务的时候\n",
    "    #new_file = f.read()\n",
    "    ## 会把锁自动释放掉，就会完成操作,不需要f.close() 操作\n",
    "    new_files = f.readline()\n",
    "#print(\"这个是使用with管理器读取的数据\",new_file)\n",
    "\n",
    "print(\"这是readlines 对象\",new_files)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  换行符 \n",
    " \n",
    "unix  linux  \\n\n",
    "windows  \\r\\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是readlines 对象 ['123\\n', '123\\n', '123\\n', '123\\n', '123\\n', '123\\n', '123\\n', '123\\n', '123\\n', '123\\n']\n",
      "['123', '123', '123', '123', '123', '123', '123', '123', '123', '123']\n",
      "['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n",
      "No Python documentation found for '123'.\n",
      "Use help() to get the interactive help utility.\n",
      "Use help(str) for help on the str class.\n",
      "\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "with open(\"filename.txt\",\"r\",encoding='utf-8')  as  f:\n",
    "\n",
    "    #完成读的任务的时候\n",
    "    #new_file = f.read()\n",
    "    ## 会把锁自动释放掉，就会完成操作,不需要f.close() 操作\n",
    "    new_files = f.readlines()\n",
    "#print(\"这个是使用with管理器读取的数据\",new_file)\n",
    "\n",
    "print(\"这是readlines 对象\",new_files)\n",
    "\n",
    "##  for 循环的简单方式  \n",
    "ts = [i.replace('\\n','')  for i in new_files]\n",
    "\n",
    "tss = [i.strip('\\n')  for i in new_files]\n",
    "\n",
    "\n",
    "print(tss)\n",
    "\n",
    "a = '123'\n",
    "print(dir(a)) #'replace  #'replace \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "utf-8\n"
     ]
    }
   ],
   "source": [
    "##  获取当前系统的编码\n",
    "import sys  \n",
    "encoding_windws= sys.getfilesystemencoding()\n",
    "print(encoding_windws)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获取的数据 只有两种形式\n",
    "\n",
    "## byte \n",
    "\n",
    "## 文本  \n",
    "a ='123' \n",
    "a = b'123' # byte 类型 不可变\n",
    "\n",
    "##bytearry 数据 \n",
    "\n",
    "## 二进制数据 \n",
    "#rb   ## 读二进制数据\n",
    "#wb   ## 写二进制数据\n",
    "   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  如何获取目录\n",
    "\n",
    " os.listdir "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['filename.txt', 'README.md', 'read_file.ipynb'] <class 'list'>\n",
      "['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']\n",
      "获取我筛选到数据.. ['filename.txt']\n",
      "简要表达式获取的数据 ['filename.txt']\n"
     ]
    }
   ],
   "source": [
    "# . \n",
    "#  .. \n",
    "## 获取当前目录下面的所有文件\n",
    "import os \n",
    "filedir = os.listdir('.')\n",
    "print(filedir,type(filedir))  \n",
    "\n",
    "## 实现过滤 \n",
    "\n",
    "\n",
    "t =  '123'\n",
    "#print(dir(t))\n",
    "#help(t.endswith)\n",
    "# endswith [判断以什么结尾]\n",
    "#S.endswith(suffix[, start[, end]]) -> bool \n",
    "fileAfter = [] \n",
    "print(dir(fileAfter))\n",
    "## 清洗以.txt 结尾的文件，获取到\n",
    "for i in filedir:\n",
    "    if i.endswith('.txt'):\n",
    "        #[]\n",
    "        fileAfter.append(i)\n",
    "    else:\n",
    "        continue\n",
    "\n",
    "\n",
    "print(\"获取我筛选到数据..\",fileAfter)\n",
    "\n",
    "## 表达式\n",
    "\n",
    "new_floder = [i for i in filedir if i.endswith('.txt')]\n",
    "\n",
    "print(\"简要表达式获取的数据\",new_floder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "trans_code  ={\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\",\"6127\":\"雪龙果\"}\n",
    "search_code = input(\"请输入你需要查询的code: \")\n",
    "\n",
    "# 改成一个迭代器 那种 [] \n",
    "for key,value in trans_code.items():\n",
    "\n",
    "    #key 7125 7126\n",
    "    #value 苹果,香蕉\n",
    "    if search_code in key or search_code in value: #存在我走下面\n",
    "\n",
    "        print(key,value)\n",
    "    else:\n",
    "        print(\"我在第二行\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['7125\\n', '7126\\n', '7127']\n",
      "清洗完的数据 ['7125', '7126', '7127']\n"
     ]
    }
   ],
   "source": [
    "trans_code  ={\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\",\"6127\":\"雪龙果\"}\n",
    "with open('input_file.txt','r',encoding='utf-8') as f:\n",
    "       ts = f.readlines()\n",
    "\n",
    "print(ts)\n",
    "\n",
    "new_ts = [i.strip('\\n') for i in ts]\n",
    "print(\"清洗完的数据\",new_ts)\n",
    "##  我要拿7125 获取的值 然后存到一个新的文件, 要查询的数据，把我需要的数据给返还回来，把查询返回回来的结果都保存下来\n",
    "#7125\n",
    "#7126\n",
    "#7127 \n",
    "#pass 占位符，没有实际含义，就是为了程序不报错\n",
    "\n",
    "##  把查询结果保存为txt,把输入条件查询txt,\n",
    "\n",
    "#### 把  trans_code  ={\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\",\"6127\":\"雪龙果\"} txt,没有实现\n",
    "\n",
    "### 后续扩展 单条件查询，我多条件也支持，我既支持查询code,我有支持查询value 中文\n",
    " \n",
    "\n",
    "\n",
    "with open('search_result.text','w',encoding='utf-8') as result_file:\n",
    "    pass \n",
    "\n",
    "    # search_code 7125 7126 等 \n",
    "    for search_code in new_ts:\n",
    "        found =False \n",
    "\n",
    "        ## {\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\",\"6127\":\"雪龙果\"} \n",
    "\n",
    "        ### key 7125  7126 7127 ,6127 \n",
    "        #value 苹果 香蕉， 橘子，雪龙果\n",
    "        for key,value in trans_code.items():\n",
    "            pass \n",
    "            if search_code  in  key or search_code in value:\n",
    "                # 把查询的结果保存下来\n",
    "                result_file.write(f\"{key}:{value}\\n\")\n",
    "                found =True  \n",
    "                #break \n",
    "\n",
    "        if not found:\n",
    "            result_file.write(f\"{search_code}: 没有找到要查询的结果\")\n",
    "            \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  search_result.text\n",
    " 把  trans_code  ={\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\",\"6127\":\"雪龙果\"} txt,没有实现 \n",
    "  命中数据也存储为txt \n",
    "##### 生成日志date函数系统自动的函数,生成日志,字符串拼接\n",
    "##### 有了日志进行数据分析,统计一周内,一天,一个月,拿个词被查询的比较多，\n",
    "\n",
    "# linux 服务器 [python2 ]，和pycharm [电脑配置低用vscode]下一节将"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.11.5 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "c261aea317cc0286b3b3261fbba9abdec21eaa57589985bb7a274bf54d6cc0a7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
